
import 'dart:async';

import 'package:flutter/services.dart';

class TySmartHome {
  static const MethodChannel _channel =
      const MethodChannel('ty_smart_home');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  // 登录App
  static Future<String> loginApp(String code, String number, String pwd) async {
    Map result = await _channel.invokeMethod("loginWithWIFI",
        {"code": code, "phoneNumber": number, "password": pwd, });
    return result["result"];
  }

  /// 用户 UID（User ID）登录采用注册和登录为一体的接口，如果注册了账号就自动登录，如果没有注册就自动注册并且登录。
  static Future<Map<String, String>> loginOrRegisterWithUid(String countryCode, String uid, String pwd) async {
    Map result = await _channel.invokeMethod("loginOrRegisterWithUid",
        {"code": countryCode, "uid": uid, "password": pwd, });

    // print("登录返回: ${result["username"]}, ${result["uid"]}");
    return {
      "result": result["result"].toString(),
      "username": result["username"].toString(),
      "uid": result["uid"].toString(),
    };
  }
  /// 以 UID 的方式注册App
  static Future<String> registerAppWithUID(String countryCode, String uid, String pwd) async {
    Map result = await _channel.invokeMethod("registerWithUID",
        {"code": countryCode, "uid": uid, "password": pwd, });
    return result["result"];
  }

  // 获取设备列表
  static Future<List> getDeviceList() async {
    List result = await _channel.invokeMethod("getDeviceList");
    // print("获取设备列表: $result");
    return result;
  }

  // 获取HomeID
  static Future<String> getHomeID() async {
    String result = await _channel.invokeMethod("getHomeID");
    return result;
  }

  // 开始配网
  static commitEZModeAction(String homeID, String ssid, String pwd) {
    _channel.invokeMethod("commitEZModeAction", {"homeID": homeID, "ssid": ssid, "password": pwd});
  }

  // 获取ssid
  static Future<String> getWIFIName() async {
    Map result = await _channel.invokeMethod("getWIFIName");
    return result["result"];
  }

  // 开启定位权限
  static startLocation() {
    _channel.invokeMethod("startLocation");
  }

  // 选中设备
  static configWithDeviceID(String deviceId) {
    _channel.invokeMethod("configWithDeviceID", {"deviceId": deviceId});
  }

  // 跳到设置界面
  static go2WIFISetting() {
    _channel.invokeMethod("go2WIFISetting");
  }

  /// 修改设备dp的值
  static Future<bool> updateDeviceDPS(String deviceId, String dpKey, Object dpValue) async {
    Map result = await _channel.invokeMethod(
          "updateDeviceDPS",
          {"devId": deviceId, "dpKey": dpKey, "dpValue": dpValue}
        );
    return result["result"] == "1";
  }

  /// 向其他用户发送共享设备的邀请
  static Future<Map> inviteShare(String deviceId, String code, String uid) async {
    Map result = await _channel.invokeMethod(
        "inviteShare",
        {"devId": deviceId, "code": code, "uid": uid}
    );
    // final shareId = int.tryParse(result["result"]) ?? 0;
    return result;
  }

  /// 确认分享邀请
  static Future<bool> confirmShareInviteShare(int shareId) async {
    Map result = await _channel.invokeMethod(
        "confirmShareInviteShare", {"shareId": shareId}
    );
    return result["result"] == "1";
  }

  /// 移除主动共享的设备
  static Future<bool> disableDevShare(String devId, int tyUid) async {
    Map result = await _channel.invokeMethod(
        "disableDevShare", {"devId": devId, "tyUid": tyUid}
    );
    return result["result"] == "1";
  }

}


















